<div class="container">

    <div class="page-header">
        <div class="row">
            <h1 class="span8"><?
                if ($post_action == 'EDIT') {
                echo 'Edit';
                }
                else
                echo 'Add';
                ?> <?= ucfirst($_GET['post_type']) ?></h1>
            <span class="span4">

            </span>
        </div>


    </div>

    <div class="row">

        <div class="span8">

            <form class="form-horizontal-qtrans" id="post" method="post" name="post">

                <input type="hidden" value="<?= $_GET['post_type']; ?>" name="post_type" id="post_type">
                <input type="hidden" value="<?= $post_action; ?>" name="post_action" id="post_action">

                <?
                if ($post_action == 'EDIT') {
                ?>
                <input type="hidden" value="<?= $post['ID']; ?>" name="post_ID" id="post_ID">
                <? }
                ?>

                <fieldset>


                    <div id="post-body">

                        <div id="post-body-content">

                            <!--TITLE-->
                            <? foreach (config_item('languages') as $key => $value): ?>

                            <div class="postarea">

                                <h3 class="qtrans_title">Title (<?= $value ?>)</h3>

                                <div class="qtrans_title_wrap">

                                    <input required type="text" name="title[]" class="qtrans_title_input" value="<? if ($post_action == 'EDIT') echo $this->strparse->find_lang_in_string($post['post_title'], $key); ?>" />

                                </div>

                            </div>

                            <? endforeach; ?>

                            <!-- CONTENT -->
                            <div class="postarea" id="postdivrich">

                                <? foreach (config_item('languages') as $key => $value): ?>

                                <div class="group">

                                    <span class="group-title"> <?= $value ?> </span>

                                    <div class="editor-holder">
                                        <textarea class="textarea_content" cols="20" rows="13" name="content[]">
                                                    <?
                                                    if ($post_action == 'EDIT')
                                                        echo $this->strparse->find_lang_in_string($post['content'], $key);
                                                    ?>
                                        </textarea>
                                    </div>          

                                </div>

                                <? endforeach; ?>

                            </div>

                        </div>

                    </div>

                    <div class="parent well" style="margin-top: 23px; margin-bottom: 23px;">
                        <h3 style="padding-left: 0;"><?= ucfirst($_GET['post_type']) ?> properties</h3>
                        <label for="parent_id" class="screen-reader-text">Parent</label>


                        <?
                        $_GET['_cur_level'] = 0; 
                        $_GET['myPost'] = $_GET['post_action']=='EDIT'?$post:array('parent_ID' => -1); 

                        function parents_finder ($parent_ID = 0){

                        $CI = & get_instance ();

                        $myPosts = $CI->posts_model->view_all_children($parent_ID,$_GET['post_type']);

                        foreach ($myPosts as $onePost):

                        $selected = '';
                        $cur_ID = $onePost['ID'];

                        if ($_GET['post_action'] == 'NEW' || $cur_ID != $_GET['post_ID']):

                        if ($_GET['post_action'] == 'EDIT' && $cur_ID == $_GET['myPost']['parent_ID'])
                        $selected = 'selected="selected"';

                        ?><option value="<?= $cur_ID ?>" <? echo $selected; ?>><?= repeat_sign('&nbsp;', $_GET['_cur_level'] * 3) . '- ' .  $onePost['name'] ?></option><?

                        if(count($CI->posts_model->view_all_children($cur_ID,$_GET['post_type']))>0):

                        $_GET['_cur_level'] ++;
                        parents_finder($cur_ID);

                        endif;

                        endif;
                        endforeach;

                        $_GET['_cur_level'] --;

                        }

                        ?>

                        <select id="parent_id" name="parent_id">
                            <option value="0" selected="selected">(no parent)</option>

                            <?= parents_finder(0); ?>
                        </select>

                        <?
                        unset($_GET['myPost']);
                        unset($_GET['_cur_level']);
                        unset($_GET['_start_level']);
                        ?>

                    </div>

                    <div class="form-actions">

                        <div class="row">

                            <? if ($post_action == 'EDIT'): ?>       

                            <div class="span1-5" id="delete-action">
                                <a class="submitdelete deletion delete_post" href="<?= base_url('posts/delete/' . $post['ID'] . '?post_type=' . $_GET['post_type']) ?>">Move to Trash</a>
                            </div>

                            <? endif; ?> 

                            <span class="<?
                                  if ($post_action == 'EDIT') {
                                      echo 'span2 ml-15';
                                  }
                                  else
                                  echo 'span4';
                                  ?> save"><button class="btn btn-primary" type="submit"><i class="icon-white icon-download-alt"></i> Save changes</button></span>
                            <span class="span4 loader"><img id="ajax-loading" class="ajax-loading" src="<?= base_url() ?>assets/img/backgnds/loading.gif" style="visibility: hidden;"></span>
                        </div>

                    </div>

                </fieldset>
            </form>

        </div>

        <div class="span4">

            <div class="hero-unit">
                <h3><?= ucfirst($_GET['post_type']) ?></h3>
                <p>
                    <a class="btn btn-large" href="<?= base_url('posts/view_all?post_type=' . $_GET['post_type']) ?>">
                        <i class="icon-list-alt"></i> View All <?= ucfirst($_GET['post_type']) ?>
                    </a>
                </p>
            </div>

        </div>
    </div>

</div> <!-- /container -->
<script>
    var post_type = '<?= $_GET['post_type'] ?>';
</script>
